let list = [];
let listNew = [];
let cat_id = '';
//减少商品数量
function subValue(){
	let value = document.querySelector(".value").value;
	value--;
	if(value>=1){
		document.querySelector(".value").value = value;
	}
	else{
		document.querySelector(".value").value = 1;
	}
}
//增加商品数量
function addValue(){
	let value = document.querySelector(".value").value;
	value++;
    document.querySelector(".value").value = value;
}
function imgScope(currentIndex,list){
	let scopeList = [];
	let start = 0;
	let end = 0;
	if(list.length > 0 && list.length <= 5){
		for(i = 0; i < list.length; i++){
			scopeList.push(list[i]);
		}
	}
	if(list.length > 5){
		start = currentIndex - 2;
		end = currentIndex + 2;
		if(start < 0){
			start = 0;
			end = 4;
		}
		if(end > list.length - 1){
			end = list.length - 1;
			start = list.length - 5;
		}
		for(let j = start; j <= end; j++){
			scopeList.push(list[j]);
		}
	}
	return scopeList;
}

function getCatName(cat_id){
	    return new Promise((resolve, reject) => {
	        ajax({
	            method: "GET",
	            url: BASE_URL + "/api_cat",
	            type: "JSON",
	            callback: function(data) {
	                let list = data.data;
	               list.forEach(item=>{
	               	if(cat_id == item.cat_id){
	               		resolve(item.cat_name);
	               	}
	               })
	            },
	            error: reject // 如果 AJAX 失败，reject 错误
	        });
	    });
}
// 渲染商品列表
function goodsList(good){
	ajax({
		method:"GET",
		url:BASE_URL + "/api_goods" + "?goodsId=" + good,
		type:"JSON",
		callback:function(data){
			cat_id = data.data[0].cat_id;
			let type = document.querySelector(".type");
			let content = '';
			//点赞数
			let like = data.data[0].star_number;
			let goodLikes = document.querySelector(".goodLikes");
			goodLikes.innerHTML = like;
			//目录
			content += ' ' + data.data[0].goods_name;
			type.innerHTML = content;
			list = data.data[0].banner;
			//商品品牌名称
			let goodBrand = document.querySelector(".goodBrand");
			let brand = data.data[0].brand_name;
			goodBrand.innerHTML = brand;
			//商品名称
			let goodName = document.querySelector(".goodName");
			let name = data.data[0].goods_name;
			goodName.innerHTML = name;
			//商品价格
			let price = document.querySelector(".price>span")
			let goodPrice = data.data[0].price;
			price.innerHTML = "¥"+goodPrice
			//渲染商品图片
			let present = document.querySelector(".present");
			let product_banner = data.data[0].product_banner;
			let produnctList = ''
			product_banner.forEach(item=>{
				produnctList += "<div class='goodDetail'>" +
				"<img src='"+item+"' class='presentImg'>" +
				"</div>"
			})
			present.innerHTML = produnctList;
			//品牌简介
			let gdIntroduction = document.querySelector(".gdIntroduction>div");
			let introList = data.data[0].lc_recommend;
			gdIntroduction.innerHTML = introList[introList.length - 1];
			//渲染大图和小图
			let listNew = imgScope(0,list);
			if(list.length > 0){
				imgId.src = list[0];
			}
			let listBox = document.querySelector(".listBox>ul");
			let text = '';
			list.forEach(item =>{
				text += "<li>" +
				"<div>" +
				"<img class='picList' src='"+item+"'>" +
				"</div>" +
				"</li>"
			})
			listBox.innerHTML = text;
			let picList = document.querySelectorAll(".picList");
			picList.forEach((item,index) =>{
				item.addEventListener("click",()=>{
					imgId.src = list[index];
				})
			})
			//猜你喜欢
			document.querySelector("#recomImg>img").src = list[0];
		}
	})
}



window.onload = function(){
	let searchParams = new URLSearchParams(window.location.search);
	let goodsId = searchParams.get('goodsId');
	goodsList(goodsId);
	let addCar = document.querySelector(".addCar"); 
	console.log(userId)
	//加入购物车
	addCar.addEventListener("click",()=>{
		let number = document.querySelector("input[class='value']").value;
		ajax({
			method:"POST",
			url:BASE_URL+"/api_cart",
			type:"JSON",
			params:"userId="+userId+"&goodsId="+goodsId+"&goodsNumber="+number+"&status=addcart",
			callback:function(data){
				alert(data.message);
			}
		})
	})
	//加入结算清单
	document.querySelector(".purchase>a").addEventListener('click',()=>{
		let number = document.querySelector("input[class='value']").value;
		ajax({
			method:"POST",
			type:"JSON",
			url:BASE_URL+"/api_settlement",
			params:"userId="+userId+"&from=product&goodsId="+goodsId+"&goodsNumber="+number+"&status=addsettlement",
			callback:function(data){
				console.log(data)
				document.querySelector(".purchase>a").href = "settlement.html";
			}
		})
	})
	
}